var Parse = require('parse').Parse;
Parse.initialize("3yE6WU6lvxXc9SyKKhtwPcpdHxutfE1wXaSShYyk", "hdLn0e9gbeFIkwa7AmkrOxm25Bco92QXk3emXXbo");
var pushMessage = {};
module.exports = function (app) {
	app.get("/shortMessage",function(req,res){
		req.session.user = {code:"001"};
		// var clockwork = require('clockwork')({key:'e3c35f368f93682b4c66fc4104ba8ccef1a437b6'});

		// clockwork.sendSms({ To: '971566083573', Content: '汤，OK!'}, 
		//   function(error, resp) {
		//     if (error) {
		//         console.log('Something went wrong', error);
		//     } else {
		//         console.log('Message sent',resp.responses[0].id);
		//     }
		// });
		// res.json({cookieid:req.session.id})
		res.json({code:1,message:'ok'});
	});

	app.get("/signIn",function(req,res){
		if(req.session.user.code == req.query.code)
			res.json({code:1,message:'success'});
		else
			res.json({code:0,message:'error code'});
	});

	app.post("/file-upload",function(req,res,next){
		res.json({path:req.files.file.path});
	});
	
	app.post("/push",function(req,res,next){
		Parse.Push.send({
			channels: ["p"+req.body.to],
			data: {
				alert: "receive one new message"
			}
			}, {
				success: function() {
					var arr = pushMessage[req.body.to]||[];
					arr.push({id:req.body.id,from:req.body.from,content:req.body.content,type:req.body.type});
					pushMessage[req.body.to] = arr;
					res.json({code:1,message:"ok"});
				},
				error: function(error) {
					res.json({code:0,message:"error"});
				}
			});
	});

	app.get("/message",function(req,res){
		res.json(pushMessage[req.query.id]);
		pushMessage[req.query.id] = null;
	});

}
